﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

public partial class Admin_NewsEdit : System.Web.UI.Page
{
    public string indID;
    protected void Page_Load(object sender, EventArgs e)
    {
        indID = Request.QueryString["id"];
        if (!IsPostBack)
        {
            string c = "";     
            int i=0;
            if (!string.IsNullOrEmpty(indID))
            {
                NewsData ind = new NewsData();
                ind.ID = new Guid(indID);
                DataTable dt = NewsDataDBOption.DataTableSelect(ind);
                DataRow dr = dt.Rows[0];
                c = dr["Category"].ToString();
                TextBox1.Text = dr["Title"].ToString();
                CheckBox1.Checked = Convert.ToBoolean(dr["Top"]);
                Insert_WebEditor.Text = dr["Txt"].ToString();
                if (c == "1")
                    DropDownList1.SelectedIndex = 0;
                else
                    DropDownList1.SelectedIndex = 1;
            }
            string sql = "SELECT ID, Name FROM Category";
            DataTable dt1 = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql);
            foreach (DataRow dr1 in dt1.Rows)
            {
                ListItem li = new ListItem(dr1["Name"].ToString(), dr1["ID"].ToString());
                DropDownList1.Items.Add(li);
                if (c == dr1["ID"].ToString())
                    i = DropDownList1.Items.Count - 1;
            }
            DropDownList1.SelectedIndex = i;

        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        NewsData ind = new NewsData();
        ind.Category = DropDownList1.SelectedValue;
        ind.EditDate = DateTime.Now;
        ind.Title = TextBox1.Text;
        ind.Top = CheckBox1.Checked;
        ind.Txt = Insert_WebEditor.Text;
        if (string.IsNullOrEmpty(indID))
            NewsDataDBOption.Insert(ind);
        else
        {
            ind.ID = new Guid(indID);
            NewsDataDBOption.Update(ind);
        }
        Response.Redirect("~/Admin/NewsList.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/login.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/login.aspx");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/SearchList.aspx?search=" + Server.UrlEncode(TextBox2.Text));
    }
}